class BillingTransaction < ActiveRecord::Base    
  belongs_to :billing_order
  serialize :params
  cattr_accessor :gateway
  
  before_validation :prepare #, :on => [:create, :update]
  # before_create :customer_transaction
 
  # operations that allows   only : new, change status for manual only
  
  # validation
  # default not manual creating any transaction
  # prepare for test mode or live mode
  
  self.gateway = ACCOUNT_GATEWAY

  def prepare
    self.ip_address = request.remote_ip
  end

  def customer_transaction(options = {})
    requires! (options, :amount, :type, :customer_profile_id, :customer_payment_profile_id)
    response = gateway.create_customer_profile_transaction({:transaction => { :type => options[:type],
                                                               :amount => options[:amount],
                                                               :customer_profile_id => options[:customer_profile_id],
                                                               :customer_payment_profile_id => options[:customer_payment_profile_id],
                                                               :order => {:invoiceNumber => self.id,
                                                                          :description => options[:note]}},
                                               :extraOptions => self.ip_address})
    end

    if response.success?
      result.message = response.params["direct_response"]["message"] + "" + response.params["direct_response"]["raw"]
      result.auth_id = response.params["direct_response"]["approval_code"]
      logger.info "BiilingTransactions::success_transaction::" + options[:type] + "::out_code-" + result.auth_id + "::" + params
    else #if response && !response.success?
      raise ZepFrog::PaymentError, response.message
    end
  end
   
   # 
   # class << self
   #   
   # 
   #   
   #   
   #   def profile_transaction(options = {})
   #     requires! (options, :amount, :type, :customer_profile_id, :customer_payment_profile_id)
   #     process(options) do |gw|
   #       gw.create_customer_profile_transaction({:transaction => { :type => options[:type],
   #                                                                :amount => options[:amount],
   #                                                                :customer_profile_id => options[:customer_profile_id],
   #                                                                :customer_payment_profile_id => options[:customer_payment_profile_id],
   #                                                                :order => {:invoiceNumber => "self.id",
   #                                                                           :description => options[:note]}},
   #                                                :extraOptions => "ip"})
   #     end
   #   end
   #   
   #   private
   #   def process(options={})
   #     # requires! (options, :type) requires!(options, :customer_profile_id, :address)
   #     
   #     result = BillingTransaction.new
   #     result.amount = options[:amount]
   #     result.kind = options[:type]
   #     result.order_id = options[:order_id]
   #     result.note = options[:note] # annual charge  return refund 
   #     result.created_at = result.updated_at = Time.now
   # 
   #     # add column user_id
   #     # result.user_id = options[:user_id]
   # 
   #     begin
   #       response = yield gateway
   #       
   #       if response.success?
   #         result.message   = response.params["direct_response"]["message"]
   #         result.note = (result.note || "") + "__" + response.params["direct_response"]["raw"] 
   #         result.auth_id = response.params["direct_response"]["approval_code"]
   #       else #if response && !response.success?
   #         raise ActiveMerchant::ActiveMerchantError, response.message
   #       end
   # 
   #       # result.success   = response.success?
   #       # result.auth_id   = (response.params["direct_response"]["raw"] || 0)
   #       # result.params    = response.params
   #       # result.test      = response.test?
   #     rescue ActiveMerchant::ActiveMerchantError => e
   #       # result.success   = false
   #       result.note = (response.nil? ? "" : (response.params["direct_response"].nil? ? "" : response.params["direct_response"]["raw"])) + "__" + (result.note || "")
   #       result.auth_id = nil
   #       result.message   = e.message
   #       # result.params    = {}
   #       # result.test      = gateway.test?
   #     end
   #     # result.save!
   #     result
   #   end
   # end
   # 
   # # 
   # #  
   # # class << self
   # #   def authorize(amount, credit_card, options = {})
   # #     process('authorization', amount) do |gw|
   # #       gw.authorize(amount, credit_card, options)
   # #     end
   # #   end
   # #   
   # #   def capture(amount, authorization, options = {})
   # #     process('capture', amount) do |gw|
   # #       gw.capture(amount, authorization, options)
   # #     end
   # #   end
   # #   
   # #   private
   # #   
   # #   def process(action, amount = nil)
   # #     result = BillingTransaction.new
   # #     result.amount = amount
   # #     result.action = action
   # #   
   # #     begin
   # #       response = yield TRANZACTION_GATEWAY
   # #   
   # #       result.success   = response.success?
   # #       result.reference = response.authorization
   # #       result.message   = response.message
   # #       result.params    = response.params
   # #       result.test      = response.test?
   # #     rescue ActiveMerchant::ActiveMerchantError => e
   # #       result.success   = false
   # #       result.reference = nil
   # #       result.message   = e.message
   # #       result.params    = {}
   # #       result.test      = TRANZACTION_GATEWAY.test?
   # #     end
   # #     
   # #     result
   # #   end
   # # end
end



# def self.purchase
# end
# 
# def self.capture
# end
# 
# def self.authorize
# end
# 
# def void
# end
# 
# def credit
# end

# CIM_TRANSACTION_TYPES = {
#   :auth_capture => 'profileTransAuthCapture',
#   :auth_only => 'profileTransAuthOnly',
#   :capture_only => 'profileTransCaptureOnly'
# }
# 
# CIM_VALIDATION_MODES = {
#   :none => 'none',
#   :test => 'testMode',
#   :live => 'liveMode'
# }
